<?php
/**
*+------------------APP 评论控制器---------------+
*--------------------------------------------------+
*+-----------进行评论添加操作------------------------+
*+-------------------------------------------------+
*+-@author susy.liang <liangling@ilongyuan.com.cn>-+
*+-------------------------------------------------+
*/
namespace Api\Controller;

class CommentsController extends PublicController 
{	
	/**
	*评论的事件对象
	*/
	protected $eventObj = '';

	/**
	*参数初始化
	*/
	protected function _initialize(){
		parent::_initialize();
		$this->eventObj = A('Api/Comments', 'Event');
	}


	// 获取最新的几条评论
	public function news()
	{
		$offset_id = I('offset_id', 0, 'intval');
		$per = I('per', 5, 'intval');
		$service = service('Comments');
		$map = [
			'status' => ['egt', $service::STATUS_PEDDING],
			'id'     => ['gt', $offset_id],
		];
		$res = $service->where($map)->limit($per)->order('id desc')->select();
		foreach ($res as $key => $value) {
			$value = field_filter($value, ['id', 'video_id', 'uid', 'nickname', 'content', 'create_time']);
			$value['video'] = event('video')->detail($value['video_id']);
			$value['user']  = event('user')->info($value['uid']);
			$value['content'] = htmlspecialchars($value['content']);
			$lists[] = $value;
		}
		$this->list_success($lists);
	}


	/**
	*新增评论信息
	*@return json
	*/
	public function add(){

		$this->checkLogin();
		$content = I('comment');

		// 添加权限判断
		$this->checkAuth('comments');
		$vid = I('vid', 0, 'intval');
		$user = array(
				'uid'=>UID, 
				'nickname'=>$this->nickname,
			);

		$flg = $this->eventObj->add($vid, $user, $content, $ret_data);

		switch ($flg) {
			case 1:
				$info ='非法视频';
				$code = self::$API_ERROR['ILLEGAL_VIDEO'];
				break;
			case 2:
				$info = '评论不能为空';
				$code = self::$API_ERROR['NULL_COMMENT'];
				break;
			case 3:
				$info = '评论失败';
				$code = self::$API_ERROR['SERVER'];
				break;
			default :
			   	$code = 200;
				$info = '评论成功';
				break;
		}

		if($code == 200)
			$this->success($info, $ret_data);
		else
			$this->error($code, $info);

	}
}